<!--  -->
<template>
  <div class="t-left">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="mt15">
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索区域 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="resetGoods">
            <el-button slot="append" icon="el-icon-search" @click="searchGoodsResult"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <!-- 添加用户按钮 -->
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品表格区域 -->
      <el-table :data="goodsList" style="width: 100%;margin-top:15px;" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="480"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="180"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{scope.row.add_time|getTime("yyyy-MM-dd hh:mm:ss")}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.row)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="mt15"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 请求参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      // 商品数据存放
      goodsList: [],
      // 总数
      total: 0,
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getGoodsList();
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  computed: {},
  methods: {
    // 获取商品数据
    getGoodsList() {
      this.$axios
        .get("goods", {
          params: this.queryInfo,
        })
        .then((val) => {
          // console.log(val);
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg);
          }
          this.goodsList = val.data.goods;
          this.total = val.data.total;
        });
    },
    // 重置搜索框
    resetGoods() {
      this.queryInfo.query='';
      this.getGoodsList()
    },
    // 点击搜索按钮
    searchGoodsResult() {
      this.getGoodsList()
    },
    // 点击添加商品按钮
    addGoods() {
      // 编程导航到 /goods/add
      this.$router.push('/goods/add');
    },
    // 点击删除按钮
    deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete(`goods/${id}`).then((val) => {
            // console.log(val);
            if (val.meta.status != 200) {
              return this.$message.error(val.meta.msg);
            }
            this.$message.success(val.meta.msg);
            this.getGoodsList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页单页条数改变
    handleSizeChange(size) {
      this.queryInfo.pagesize = size;
      this.getGoodsList();
    },
    // 页码改变
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num;
      this.getGoodsList();
    },
    // 修改商品
    editGoods(row){
      console.log(row);
    }
  },
};
</script>
<style scoped>
/* @import url(); 引入css类 */
.mt15 {
  margin-top: 15px;
}
</style>